2.6 Indexing
Indexing provides access to elements of a composite or indexable collection value. The indices are provided in a comma-separated list delimited by brackets situated as a postfix operator on an expression (see Figure 2.24). The expression being indexed is called an indicand. An alternative syntax simplifies indexing for composites. Sets, although collections, are not indexable because indexing can only be applied to an expression whose items are ordered. A double index applied to a matrix evaluates to an element of the matrix. A singleton index applied to a matrix evaluates to a tuple representing a row of the matrix.
Indices can be applied as a list but they can also be applied one at a time by separating the list into singleton indexes.
Note the difference between
Although indexes can be applied to value expressions, they are most commonly applied to variables. Because of Myron's late binding semantics, the value of an indexed variable is not known until evaluation time. This means that indexes cannot be strongly typed. The result is that an index can be applied to almost any type of indicand expression and also means that some index expressions cannot be evaluated.
For example, consider the tuple of scalars
2.6.1 Indexing generators
A generator or a variable bound to a generator is indexed by acquiring the indexth
item in the domain sequence and then evaluating the template, as bound to that item.
For example,
2.6.2 Indexing composite values
Complex, radial and vector variables and values can be indexed. Index 0 on complex values produces the real part and
index 1 produces the imaginary part. Vectors are indexed such that index 0 produces the first, or x, component. Radials differ in that
the magnitude, although the first component of a radial value, cannot be accessed by index 0. That index extracts the first angle, while
index 1 extracts the second angle and so on. The magnitude of a radial is accessed by magnitude notation:
2.6.3 Alternate indexing for composites
Vector. The x component of a vector
Complex. Using the techniques for vectors, the components of a complex number can be extracted using named complex components r and i. As with vectors, if the right operand is a real variable, it is rewritten as a complex variable.
Radial. Again following the technique for vectors, the angles of a radial value (or a real variable rewritten as a radial variable) can be extracted using named radial angles θ and φ. Further angles for higher dimensional radials must be extracted using regular indexing. The magnitude can be extracted using the magnitude operator.
In summary, the indicand in an expression of the form
Component | Indicand |
x, y, z | vector |
i, r | complex |
θ, φ | radial |